package com.example.modulepicselector.view.activity;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Parcelable;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.example.modulepicselector.R;
import com.example.modulepicselector.adapter.PicAdapter;
import com.example.modulepicselector.internal.entity.Item;
import com.example.modulepicselector.internal.entity.SelectionSpec;

import com.example.modulepicselector.internal.model.PicCollection;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

import static android.widget.NumberPicker.OnScrollListener.SCROLL_STATE_IDLE;

public class MatActivity extends AppCompatActivity implements PicCollection.PicCallbacks, PicAdapter.OnRecyclerViewItemClickListener, View.OnClickListener {
    private RecyclerView mRvListPic;
    private final PicCollection mPicCollection = new PicCollection();
    private PicAdapter mPicAdapter;
    public static boolean sIsViewIdle;
    private TextView mTvPreView;

    List<Item> mItems;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mat);
        initView();
        SelectionSpec spec = SelectionSpec.getInstance();
        mPicAdapter = new PicAdapter(this, mRvListPic);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 3);
        mRvListPic.setLayoutManager(gridLayoutManager);
        mPicAdapter.setOnItemClickListener(this);

        mRvListPic.setAdapter(mPicAdapter);
        mPicCollection.onCreate(this, this);
        mPicCollection.onRestoreInstanceState(savedInstanceState);
        mPicCollection.loadPic();
        final boolean[] isFirstCreate = {true};

        mRvListPic.setOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(@NonNull @NotNull RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);

                if (isFirstCreate[0]) {
                    mPicAdapter.notifyDataSetChanged();

                }


                if (newState == SCROLL_STATE_IDLE) {
                    sIsViewIdle = true;
                    mPicAdapter.notifyDataSetChanged();
                } else {
                    sIsViewIdle = false;
                }

                isFirstCreate[0] = false;
            }
        });
    }

    private void initView() {

        mRvListPic = findViewById(R.id.rv_pic_list);

        mTvPreView = findViewById(R.id.button_preview);
        mTvPreView.setOnClickListener(this);
    }


    @Override
    public void onPicLoad(Cursor cursor) {

        mPicAdapter.swapCursor(cursor);
        Handler handler = new Handler(Looper.getMainLooper());
        handler.post(() -> {

        });
    }

    @Override
    public void onPicReset() {

    }


    @Override
    public void onItemClick(List<Item> selectItems) {
        this.mItems = selectItems;
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.button_preview) {
            if (mItems.isEmpty()) {
                Toast.makeText(this, "请选择图片", Toast.LENGTH_SHORT).show();
                return;

            }
            Intent intent = new Intent(this, PagerActivity.class);
            intent.putParcelableArrayListExtra("select_items", (ArrayList<? extends Parcelable>) mItems);
            startActivityForResult(intent, 110);
        }
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable @org.jetbrains.annotations.Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        System.out.println("可以吗");
        switch (requestCode) {
            case 110:
                if (resultCode == RESULT_OK) {
                    System.out.println("haha" + data.getIntExtra("haha", 0));

                }
                break;
        }
    }
}